在 VBA 中检查一个字符串是否包含一个子字符串 | 您所在的位置:网站首页 › vba split函数用法 › 在 VBA 中检查一个字符串是否包含一个子字符串 |
当前位置:主页 > 学无止境 > 编程语言 >
WEB前端
编程语言
网络
算法
操作系统
数据库
在 VBA 中检查一个字符串是否包含一个子字符串
作者:迹忆客
最近更新:2023/03/19
浏览次数:
本文将演示使用 Instr() 函数、InstrRev() 函数和 Like 函数来检查主字符串是否包含子字符串。 使用 Instr() 函数检查主字符串是否包含子字符串Instr() 函数语法: InStr([ start ], string1, string2, [ compare ])返回类型:整数 参数: [ start ] 可选的。搜索将开始的数值。对于 [ start ] 参数,以下是相应的值: 1 - [默认] 搜索将从主字符串的开头开始< br/> n - 搜索将从 n 位置开始。 string1 强制的。要搜索的字符串(主字符串) string2 强制的。要查找的字符串。 [ compare ] 可选的。指示将使用哪种字符串比较方法。对于 [ compare ] 参数,以下是相应的值: 0 - [默认] 二进制比较方法(区分大小写) 1 - 文本比较方法(不区分大小写)下面的代码块将使用 Instr() 函数检查子字符串是否在 VBA 的主字符串中。 Function IsSubstring(pos as Integer, mainStr as String, subStr as String,compTyp as Integer) as boolean 'if `Instr()` function returned 0 then the substring is not present in the main string. 'If `Instr()` function returned a value greater than `0`, would mean that the substring is in the main string. If Instr(pos,mainStr,subStr,compTyp) >0 Then IsSubstring = true Else: IsSubstring = false End if End Function Sub test1() Debug.print IsSubstring(1,"ABCDE","C",1) End Sub Sub test2() Debug.print IsSubstring(1,"ABCDE","F",1) End Sub Sub test3() Debug.print IsSubstring(1,"ABCDE","c",0) End Sub输出 test1: True输出 test2: False输出 test3: False下面的代码块将使用 Instr() 函数从主字符串返回子字符串的位置。 Function GetPosition(pos as Integer, mainStr as String, subStr as String,compTyp as Integer) 'Check first if the substring is in the main string. If InStr(pos, mainStr, subStr, compTyp) > 0 Then 'if substring is in the main string then get the position of the substring in the main string. GetPosition = InStr(1, mainStr, subStr, 1) Else: GetPosition = ("Subtring is not in the main string.") End If End Function Sub test1() 'Check if `C` is in `ABCDE` starting at the first letter (A), case insensitive. Debug.Print GetPosition(1,"ABCDE", "C",1) End Sub Sub test2() 'Check if `c` is in `ABCDE` starting at the first letter of the main string, case sensitive. Debug.Print GetPosition(1,"ABCDE", "c",0) End Sub Sub test3() 'Check if `c` is in `ABCDE` starting at the fourth letter of the main string, case sensitive. Debug.Print GetPosition(4,"ABCDE", "c",0) End Sub输出 test1: 3输出 test2: Subtring is not in the main string.输出 test3: Subtring is not in the main string. 使用 InstrRev() 函数检查主字符串是否包含子字符串InstrRev() 函数语法: InStrRev(string1, string2,[ start ], [ compare ])返回类型:整数 参数: string1 强制的。要搜索的字符串(主字符串) string2 强制的。要查找的字符串。下面的代码块将使用 InstrRev() 函数检查子字符串是否在 VBA 的主字符串中。 Function IsSubstring(mainStr As String, subStr As String) As Boolean 'if `InstrRev()` function returned 0 then the substring is not present in the main string. 'If `InstrRev()` function returned a value greater than `0`, would mean that the substring is in the main string. If InStrRev(mainStr, subStr) > 0 Then IsSubstring = True Else: IsSubstring = False End If End Function Sub test1() Debug.Print IsSubstring("ABCDE", "C") End Sub Sub test2() Debug.Print IsSubstring("ABCDE", "F") End Sub输出 test1: True输出 test2: False 使用 Like 运算符检查主字符串是否包含子字符串Like 运算符语法: res = string Like pattern返回类型:布尔值 参数: res 强制的。布尔返回值 string 强制的。要查看的字符串 pattern 强制的。要查找的字符串。更多详情见备注备注: ? 任何单个字符 * 任意 0 到多个字符 # 任何单个数字(0 到 9)下面的代码块将使用 Like 运算符检查子字符串是否在 VBA 的主字符串中 Function IsSubString(mainStr as String,subStr as String) as Boolean 'Check if subStr is in the main string by using * If mainStr Like "*" & subStr & "*" Then IsSubString = True Else: IsSubstring= False End If End Function Sub test1() Debug.print (IsSubString("ABCDE","C")) End Sub Sub test2() Debug.print (IsSubString("ABCDE","c")) End Sub Sub test3() Debug.print (IsSubString("ABCDE","F")) End Sub输出 test1: True输出 test2: False输出 test3: False上一篇:在 VBA 中对数组和 Arraylist 的元素进行排序 下一篇:在 VBA 中调用 Sub 相关文章 使用 Excel VBA 运行 SQL 查询发布时间:2023/03/19 浏览次数:78 分类:编程语言 本教程演示了使用 Excel VBA 运行 SQL 查询。 使用 Excel VBA 将数据写入文本文件发布时间:2023/03/19 浏览次数:109 分类:编程语言 本教程演示如何使用 VBA 生成文本文件。 VBA 中的循环和退出循环发布时间:2023/03/19 浏览次数:135 分类:编程语言 本教程演示如何在 VBA 中退出 For、Do Until、Do While 循环。 VBA 中的单元格选择发布时间:2023/03/19 浏览次数:160 分类:编程语言 本教程演示如何在 VBA 中选择单元格。 在 VBA 中查找最后一行和最后一列发布时间:2023/03/19 浏览次数:61 分类:编程语言 本教程演示如何在 Excel VBA 中获取上次使用的行数和列数。 在 VBA 中计算列数发布时间:2023/03/19 浏览次数:61 分类:编程语言 本教程演示如何计算 VBA 中的列数。 在 VBA 中更改单元格颜色发布时间:2023/03/19 浏览次数:99 分类:编程语言 我们将演示如何在 VBA 中更改单元格颜色。 VBA 中具有多个条件的自动过滤器发布时间:2023/03/19 浏览次数:74 分类:编程语言 本教程演示了如何在 VBA 中使用具有多个条件的自动过滤器。 在 VBA 中设置工作表发布时间:2023/03/19 浏览次数:181 分类:编程语言 本教程演示如何在 VBA 中设置工作表。 转载请发邮件至 [email protected] 进行申请,经作者同意之后,转载请以链接形式注明出处 本文地址: |
CopyRight 2018-2019 实验室设备网 版权所有 |